from zeep import Client
import time

ip = '47.102.200.101'
port = 9000

def loadClient():
    try:
        client = Client("http://%s:%s/?wsdl" % (ip, port))
    except:
        loadClient()
    return client


#TODO 需要对用户密码进行MD5加密
def login(userName,loginIp,loginMac,password):
    '''
    :param userName:  用户名从
    :param loginIp:   登录IP从
    :param loginMac:
    :param password:
    :return:
    '''

    try:
        client = loadClient()
        r = client.service.selectUserPasswordByName(str(userName),str(loginIp),str(loginMac))
        if (r[-1]=="200"):
            if(password==r[0]):
                return "True"
            #TODO 添加更详细的登录问题
            else:
                return str(r[-1])
    except:
        return "False"


def relodeTable():
    try:
        client = loadClient()
        r = client.service.selectTrain()
        if r[-1] == "200":
            return r[:-1]
        else:
            return "1"

    except:
        return "try 失败"

def getData(id):
    try:
        client = loadClient()
        r = client.service.selectTrainByDemandOrderNumber("user1","ip","mac",id)

        if r[-1] == "200":
            return r[:-1]
        else:
            return "False"

    except:
        return "False"


def insertData(id, id2, number1, number2, number3):
    try:
        client = loadClient()
        r = client.service.insertTrainByDemandOrderNumber("user1", "ip", "mac", id, id2, number1, number2, number3)

        if r[-1] == "200":
            return r[:-1]
        else:
            return "False"
    except:
        return "False"

def vagueSelectByDemandOrderNumber(id):
    try:
        client = loadClient()
        r = client.service.selectTrainByLikeDemandOrderNumber("user1", "ip", "mac", id)

        if r[-1] == "200":
            return r[:-1]
        else:
            return "False"

    except:
        return "False"

def vagueSelectByWaybillId(id):
    try:
        client = loadClient()
        r = client.service.selectTrainByLikeWaybillId("user1", "ip", "mac", id)

        if r[-1] == "200":
            return r[:-1]
        else:
            return "False"


    except:
        return "False"
